const categoryModel = require('../models/category.model')
const copyCategoryList = require('../utils/copyList')

class CategoryController {

  async list(ctx, next) {
    const result = await categoryModel.getCategoryList()

    ctx.body = {
      returnCode: 200,
      data: result
    }
  }

  async categoryDetail(ctx, next) {
    const categoryId = ctx.request.params.id

    const result = await categoryModel.getCategoryDetali(categoryId)
    const copiedCategoryList = copyCategoryList(result[0].goodsList, 8)
    const copiedCategoryListLength = copiedCategoryList.length
    result[0].goodsList = copiedCategoryList

    ctx.body = {
      returnCode: 200,
      data: Object.assign({}, result[0], {copiedCategoryListLength})
    }
  }

}

module.exports = new CategoryController()
